//时间戳
function toDate(number) {
    if ((number + "").length == 13) {
        var n = number;
    } else if ((number + "").length == 10) {
        var n = number * 1000;
    }
    var date = new Date(n);
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    return (Y + M + D)
    var h = date.getHours() + ':';
    var m = date.getMinutes();
    var s = date.getSeconds();
}
function toDate2(number) {
  if ((number + "").length == 13) {
    var n = number;
  } else if ((number + "").length == 10) {
    var n = number * 1000;
  }
  var date = new Date(n);
  var Y = date.getFullYear() + '-';
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  var h = date.getHours() + ':';
  var m = date.getMinutes() + ':';
  var s = date.getSeconds();
  return (h + m + s)
}
function toDate3(number) {
  if ((number + "").length == 13) {
    var n = number;
  } else if ((number + "").length == 10) {
    var n = number * 1000;
  }
  var date = new Date(n);
  var Y = date.getFullYear() + '-';
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate())+' ';
  var h = date.getHours() + ':';
  var m = date.getMinutes() + ':';
  var s = date.getSeconds();
  return (Y+M+D+h + m + s)
}
function formatSeconds(value) {
/*   var secondTime = parseInt(value);// 秒
  var minuteTime = 0;// 分
  var hourTime = 0;// 小时
  if (secondTime > 60) {//如果秒数大于60，将秒数转换成整数
    //获取分钟，除以60取整数，得到整数分钟
    minuteTime = parseInt(secondTime / 60);
    //获取秒数，秒数取佘，得到整数秒数
    secondTime = parseInt(secondTime % 60);
    //如果分钟大于60，将分钟转换成小时
    if (minuteTime > 60) {
      //获取小时，获取分钟除以60，得到整数小时
      hourTime = parseInt(minuteTime / 60);
      //获取小时后取佘的分，获取分钟除以60取佘的分
      minuteTime = parseInt(minuteTime % 60);
    }
  }
  var result = "" + parseInt(secondTime) + "秒";

  if (minuteTime > 0) {
    result = "" + parseInt(minuteTime) + "分" + result;
  }
  if (hourTime > 0) {
    result = "" + parseInt(hourTime) + "小时" + result;
  } 
  return result;*/
  //var hours = parseInt((value % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = parseInt((value % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = parseInt( (value % (1000 * 60)) / 1000);
  return minutes + " 分钟 " + seconds + " 秒 ";
}


//异步存值
function setStorage(key, value) {
    wx.setStorage({
        key: key,
        data: value,
    })
}

//异步取值
function getStorage(key) {
    wx.getStorage({
        key: key,
        success: function (res) {
            console.log(res.data)
        }
    })
}
//同步存值
function setStorageSync(key, value) {
    try {
        wx.setStorageSync(key, value)
    } catch (e) {
    }
}
//同步取值
function getStorageSync(rtn) {
    try {
        var value = wx.getStorageSync(rtn)
        if (value) {
            return value;
            // Do something with return value
        }
    } catch (e) {
        // Do something when catch error
    }
}


//清空缓存
function clearStorage() {

    var userid = getStorageSync("userid");
    var orgid = getStorageSync("orgid");
    var openid = getStorageSync("openid");
    var name = getStorageSync("name");
    var rid = getStorageSync("rid");
    var role = getStorageSync("role");

    console.log(userid, orgid);
    wx.clearStorage()
    setStorage("userid", userid);
    setStorage("orgid", orgid);
    setStorage("name", name);
    setStorage("rid", rid);
    setStorage("role", role);
    setStorage("openid", openid);

    

}

//分享
function onShareAppMessage(title, path){
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: title,
      path: path,
      success: function (res) {
        return res;
        // 转发成功
      },
      fail: function (res) {
        return res;        
        // 转发失败
      }
    }
}

//普通弹窗
function showToast(msg, icon) {
  wx.showToast({
    title: msg,
    icon: icon,
    duration: 2000,
    mask:true,
  })
}
//需手动关闭的弹窗
function showLoading(msg, icon) {
  wx.showLoading({
    title: msg,
    icon: icon,
    mask: true,
  })
}
//关闭弹窗
function hideLoading() {
  wx.hideLoading()
}
//选择弹窗
function modalcnt(title, content) {
  wx.showModal({
    title: title,
    content: content,
    success: function (res) {
      if (res.confirm) {
        console.log('用户点击确定')
      } else if (res.cancel) {
        console.log('用户点击取消')
      }
    }
  })
}

// 获取用户信息
function Getorgid(){
  let orgid = this.getStorageSync("orgid")
  let userid = this.getStorageSync("userid")
  let openid = this.getStorageSync("openid")
  let rid = this.getStorageSync("rid")
  let role = this.getStorageSync("role")  
  let Getorgid = new Object();
  Getorgid.orgid = orgid;
  Getorgid.userid = userid;
  Getorgid.rid = rid;
  Getorgid.role = role;
  Getorgid.openid = openid;
  return Getorgid;
}

const recorderManager = wx.getRecorderManager()
const innerAudioContext = wx.createInnerAudioContext()

function startRecord() {
  const options = {
    duration: 10000,//指定录音的时长，单位 ms
    sampleRate: 16000,//采样率
    numberOfChannels: 1,//录音通道数
    encodeBitRate: 96000,//编码码率
    format: 'mp3',//音频格式，有效值 aac/mp3
    frameSize: 50,//指定帧大小，单位 KB
  }
  //开始录音
  recorderManager.start(options);
  recorderManager.onStart(() => {
    console.log('recorder start')
  });
  //错误回调
  recorderManager.onError((res) => {
    console.log(res);
  })
}

//停止录音
function stopRecord() {
  recorderManager.stop();
  recorderManager.onStop((res) => {
    this.tempFilePath = res.tempFilePath;
    console.log('停止录音', res.tempFilePath)
    const { tempFilePath } = res
  })
}

//播放声音
function playRecord () {

  innerAudioContext.autoplay = true
  innerAudioContext.src = this.tempFilePath,
    innerAudioContext.onPlay(() => {
      console.log('开始播放')
    })
  innerAudioContext.onError((res) => {
    console.log(res.errMsg)
    console.log(res.errCode)
  })

}

//播放音乐 
 function playMusic(src){
  const innerAudioContext = wx.createInnerAudioContext()
  innerAudioContext.autoplay = true
  innerAudioContext.src = src
  innerAudioContext.onPlay(() => {
    console.log('开始播放')
  })
  innerAudioContext.onError((res) => {
    console.log(res.errMsg)
    console.log(res.errCode)
  })
} 

// 数组去重
function unique(arr) {
  var result = [], hash = {};
  for (var i = 0, elem; (elem = arr[i]) != null; i++) {
    if (!hash[elem]) {
      result.push(elem);
      hash[elem] = true;
    }
  }
  return result;
}




module.exports = {
    toDate: toDate,
    toDate2: toDate2,
    toDate3: toDate3,
    setStorage: setStorage,
    getStorage: getStorage,
    clearStorage: clearStorage,
    onShareAppMessage: onShareAppMessage,
    showToast: showToast,
    showLoading:showLoading,
    hideLoading: hideLoading,
    modalcnt: modalcnt,
    setStorageSync: setStorageSync,
    getStorageSync: getStorageSync,
    Getorgid: Getorgid,
    startRecord: startRecord,
    stopRecord: stopRecord,
    playRecord: playRecord,
    formatSeconds: formatSeconds,
    playMusic: playMusic,
    unique: unique,


}
